#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import sys
from apt import debfile
# *************************************************************************
#   > File Name : debinfo.py
#   > Author : chengzb
#   > Mail : 
#   > Created Time : 2015-08-28
#   > Comment : 
# ********************************************************************

#获取deb包里的相关信息
def getDebInfo(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_name = debgetitem(debpkg,"Package")
    deb_version = debgetitem(debpkg,"Version")
    deb_arch = debgetitem(debpkg,"Architecture")
    deb_homepage = debgetitem(debpkg,"Homepage")
    deb_description = debgetitem(debpkg,"Description")
    deb_essential = debgetitem(debpkg,"Essential")
    deb_section = debgetitem(debpkg, "Section")
    deb_maintainer = debgetitem(debpkg,"Maintainer")
    deb_installed_size = debgetitem(debpkg,"Installed-Size")
    deb_depends = debgetitem(debpkg,"Depends")
    deb_predepends = debgetitem(debpkg,"Pre-Depends")
    deb_recommends = debgetitem(debpkg,"Recommends")
    deb_suggests = debgetitem(debpkg,"Suggests")
    deb_breaks = debgetitem(debpkg,"Breaks")
    deb_conflicts = debgetitem(debpkg,"Conflicts")
    deb_replaces = debgetitem(debpkg,"Replaces")
    deb_enhances = debgetitem(debpkg,"Enhances")
    deb_provides = debgetitem(debpkg,"Provides")
    deb_xulappid = debgetitem(debpkg,"Xul-Appid")
    deb_info = deb_name+"&"+deb_version+"&"+deb_arch+"&"+deb_maintainer+"&"+deb_installed_size+"&"+deb_section+"&"+deb_homepage
    #print deb_info
    return deb_info

def getDebDescription(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_description = debgetitem(debpkg,"Description")
    result = ' '.join(deb_description.split())
    #print deb_description
    return result

def getDebName(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_name = debpkg.__getitem__("Package")
    #print deb_name
    return deb_name

def getDebVersion(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_version = debpkg.__getitem__("Version")
    #print deb_version
    return deb_version

def getDebArch(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_arch = debpkg.__getitem__("Architecture")
    #print deb_arch
    return deb_arch

def getDebMaintainer(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_maintainer = debpkg.__getitem__("Maintainer")
    #print deb_maintainer
    return deb_maintainer

def getDebInstalledSize(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_installed_size = debpkg.__getitem__("Installed-Size")
    #print deb_installed_size
    return deb_installed_size

def getDebSection(debfile_path):
    debpkg = debfile.DebPackage(debfile_path)
    deb_section = debpkg.__getitem__("Section")
    #print deb_section
    return deb_section


#获得deb数据，没有该项则返回空值
def debgetitem(debpkg,key):
    #---------------------------------------------- if debpkg.__contains__(key):
        #---------------------------------------- return debpkg.__getitem__(key)
    sections = debpkg._sections
    if key in sections:
        return debpkg.__getitem__(key)
    else:
        return "Unknown"


if __name__=="__main__":
    getDebVersion("/home/chengzb/下载/deb包集合/filezilla_3.9.0.5_arm64.deb")



